<template>
  <div class="app-container customer-div">
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="客户名称：">
          <el-autocomplete
            clearable
            class="inline-input"
            value-key="customerName"
            :fetch-suggestions="querySearch"
            v-model="listQuery.customerName"
            placeholder="请输入内容"
            @select="handleSelectCustomerName"
            @clear="clearInput"
            :trigger-on-focus="false"
          ></el-autocomplete>
        </el-form-item>

        <el-form-item label="物料名称：" >
          <el-select v-model="listQuery.goodsKingNumber" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in goodsList"
              :key="item.kingNumber"
              :label="item.goodsSn"
              :value="item.kingNumber">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
          <el-button
            @click="showDialogVisible('add','')"
            type="primary">
            新增
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container" style="margin-top:0">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%;">
        <el-table-column label="公司名称" min-width="200" align="center">
          <template slot-scope="scope">{{scope.row.customerName}}</template>
        </el-table-column>
        <el-table-column  label="物料名称" min-width="200" align="center">
          <template slot-scope="scope">{{scope.row.goodsName}}</template>
        </el-table-column>
        <el-table-column  label="大类编码" min-width="150" align="center">
          <template slot-scope="scope">{{scope.row.classCode}}</template>
        </el-table-column>
        <el-table-column  label="开票类别" min-width="150" align="center">
          <template slot-scope="scope">{{scope.row.className}}</template>
        </el-table-column>
        <el-table-column  label="牌号" min-width="150" align="center">
          <template slot-scope="scope">{{scope.row.goodsSn}}</template>
        </el-table-column>
        <el-table-column
          width="100"
          fixed="right"
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-button
              style="margin-left:0"
              type="text"
              size="mini"
              @click="showDialogVisible('edit',scope.row)">修改
            </el-button>
            <el-button
              style="margin-left:0"
              type="text"
              size="mini"
              @click="del(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

    <!--新增客户商品-->
    <el-dialog
      :close-on-click-modal="false"
      title="客户商品信息"
      :visible.sync="dialogVisible"
      width="400px">
      <el-form size="mini" label-width="120px"  ref="addForm" :rules="rules" :model="addForm">
        <el-form-item label="公司名称：" prop="customerName">
          <el-autocomplete
            clearable
            class="inline-input"
            value-key="customerName"
            :fetch-suggestions="querySearch"
            v-model="addForm.customerName"
            placeholder="请输入内容"
            @select="handleSelectCustomerName2"
            :trigger-on-focus="false"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="物料名称：" prop="goodsKingNumber" >
          <el-select v-model="addForm.goodsKingNumber" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in goodsList"
              :key="item.kingNumber"
              :label="item.goodsSn"
              :value="item.kingNumber">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开票大类编码：" prop="classCode">
            <el-input v-model="addForm.classCode" ></el-input>
        </el-form-item>
        <el-form-item label="开票类别：" prop="className">
           <el-input v-model="addForm.className" ></el-input>
        </el-form-item>
        <el-form-item label="牌号：" prop="goodsSn">
           <el-input v-model="addForm.goodsSn" ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose" size="small">取 消</el-button>
        <el-button type="primary" @click="handleConfirm" :loading="btnLoading" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {getCustomerOption,customGoodsEdit,getOrderUsersList,getDeliverDetailList,exportDeliverDetailList,orderGoodsList,customGoodsDel} from '@/api/partner'
import {formatDate} from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'
import {customGoodsList} from "../../api/partner";
const defaultListQuery={
  pageNum: 1,
  pageSize: 10,
  customerKingNumber:"",
  goodsKingNumber:"",
};
const defaultAddform={
  classCode: "",
  className: "",
  customerKingNumber: "",
  customerName: "",
  goodsKingNumber: "",
  goodsName: "",
  goodsSn: "",
  recordId: 0
};
export default {
  name: "index",
  data(){
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      roles:store.getters.roles ? store.getters.roles : [],
      goodsList:[],
      dialogVisible:false,
      addForm:JSON.parse(JSON.stringify(defaultAddform)),
      btnLoading:false,
      rules:{
        classCode:[{required: true, trigger: 'blur' ,message: '此项必填',}],
        className:[{required: true, trigger: 'blur' ,message: '此项必填',}],
        customerName:[{required: true, trigger: 'blur' ,message: '此项必填',}],
        goodsKingNumber:[{required: true, trigger: 'blur' ,message: '此项必填',}],
        goodsSn:[{required: true, trigger: 'blur' ,message: '此项必填',}],
      },
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  created() {
    this.getList();
    this.getOrderGoodsList();
  },
  filters: {

  },
  methods:{
    del(item){
      this.$confirm('确定删除此项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        customGoodsDel({recordId:item.recordId}).then(()=>{
          this.getList();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },

    //选择客户名称
    handleSelectCustomerName2(item){
      this.addForm.customerName = item.customerName;
      this.addForm.customerKingNumber = item.customerKingNumber;
      console.log("this.addForm",this.addForm)
    },
    showDialogVisible(type,item){
      this.dialogVisible = true;
      if(type == 'edit'){
        this.addForm = JSON.parse(JSON.stringify(item));
      }else{
        this.addForm = JSON.parse(JSON.stringify(defaultAddform));
      }
    },

    //关闭确认发货弹窗
    handleClose(){
      this.dialogVisible = false;
    },

    //提交修改信息
    handleConfirm(){
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let data = {};
          data.recordId = this.addForm.recordId;
          data.customerKingNumber = this.addForm.customerKingNumber;
          data.goodsKingNumber = this.addForm.goodsKingNumber;
          data.className = this.addForm.className;
          data.classCode = this.addForm.classCode;
          data.goodsSn = this.addForm.goodsSn;
          customGoodsEdit(data).then(()=>{
            this.btnLoading = false;
            this.handleClose();
            this.getList();
          }).catch(()=>{
            this.btnLoading = false;
          })
        }
      })
    },

    getOrderGoodsList(){
      orderGoodsList({hasKingNumber:1}).then((res)=>{
        this.goodsList = res.list
      })
    },

    //客户名称输入匹配
    querySearch(queryString, cb){
      getCustomerOption({keywords:queryString,hasKingNumber:1}).then((res)=>{
        cb(res);
      })
    },


    //选择客户名称
    handleSelectCustomerName(item){
      this.listQuery.customerName = item.customerName;
      this.listQuery.customerKingNumber = item.customerKingNumber
    },
    clearInput(){
      this.listQuery.customerName = "";
      this.listQuery.customerKingNumber = ""
    },

    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取列表
    getList() {
      this.listLoading = true;
      customGoodsList(this.listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
      }).catch(()=>{
        this.listLoading = false;
      });
    },

  },
}
</script>

<style scoped lang="scss">

.upload-div{
  text-align: center;
  position:relative;
  #upload{

    opacity: 0;
    position: absolute;
    z-index: 1;
    width:154px;
    height:40px
  }
}
</style>
